package com.cart.protocol;

import android.util.Log;

import com.cart.event.ReadSerialEvent;
import com.cart.protocol.analyze.CirculatingPumpRespAnalyze;
import com.cart.protocol.analyze.Co2RespAnalyze;
import com.cart.protocol.analyze.CultureMediumRespAnalyze;
import com.cart.protocol.analyze.CultureTankRespAnalyze;
import com.cart.protocol.analyze.DoorRespAnalyze;
import com.cart.protocol.analyze.ElectronicBalanceRespAnalyze;
import com.cart.protocol.analyze.FeedLiquorPumpRespAnalyze;
import com.cart.protocol.analyze.IncubatorRespAnalyze;
import com.cart.protocol.analyze.LiquidLevelRespAnalyze;
import com.cart.protocol.analyze.MagneticStirrerRespAnalyze;
import com.cart.protocol.analyze.OxygenRespAnalyze;
import com.cart.protocol.analyze.RecyclingPumpRespAnalyze;
import com.cart.protocol.analyze.RockingValveRespAnalyze;
import com.cart.protocol.analyze.SalineRespAnalyze;
import com.cart.protocol.analyze.TemperatureRespAnalyze;
import com.cart.protocol.analyze.UltravioletRespAnalyze;
import com.cart.protocol.analyze.WasteLiquorPumpRespAnalyze;
import com.cart.utils.FormatUtils;

import de.greenrobot.event.EventBus;


/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2020-01-15
       Time     :  16:18
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class CommandAnalyzeFactory extends CommandFactory {

    public static void analyzeCommand(String totalCommand) {
        //指令掐头去尾
        String content = getContent(totalCommand);
        System.out.println("content is " + content);
        //ba0f0000ffffffcc00fded
        //0f0000ffffffcc00
        int position = commandLengthContent.length() + agentNumber.length() + macAddress
                .length() + 2;

        if(position>=content.length()){
            Log.i("CommandAnalyzeFactory","数据异常，重置数据");
            return;
        }

        String readRealDeviceInfoStr = content.substring(position);
        System.out.println("read  " + FormatUtils.formatterDataToShow(readRealDeviceInfoStr));
        String deviceName = readRealDeviceInfoStr.substring(2, 6);

        switch (deviceName.toUpperCase()) {
            case ProtocolDefine.ModuleName.DOOR:
                new DoorRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.OXYGEN:
                new OxygenRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.ULTRAVIOLET:
                new UltravioletRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.ROCKING_VALVE:
                new RockingValveRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.CO2:
                new Co2RespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.TEMPERATURE:
                new TemperatureRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.SALINE_VALVE:
                new SalineRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.CIRCULATING_PUMP:
                new CirculatingPumpRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.MAGNETIC_STIRRER:
                new MagneticStirrerRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.RECYCLING_PUMP:
                new RecyclingPumpRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.WASTE_LIQUOR_PUMP:
                new WasteLiquorPumpRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.FEED_LIQUOR_PUMP:
                new FeedLiquorPumpRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.CULTURE_MEDIUM_VALVE:
                new CultureMediumRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.LIQUID_LEVEL:
                new LiquidLevelRespAnalyze(readRealDeviceInfoStr).report();
                break;
            //培养箱
            case ProtocolDefine.ModuleName.INCUBATOR_AIR_EXCHAGNE_PUMP:
                new IncubatorRespAnalyze(readRealDeviceInfoStr).report();
                break;
            //培养罐
            case ProtocolDefine.ModuleName.CULTURE_TANK_AIR_EXCHANGE_PUMP:
                new CultureTankRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.ELECTRONIC_BALANCE:
                new ElectronicBalanceRespAnalyze(readRealDeviceInfoStr).report();
                break;
            case ProtocolDefine.ModuleName.SYSTEM_POWER:
                EventBus.getDefault().post(new ReadSerialEvent("系统关机"));
                break;
            default:
                break;
        }
    }

    private static String getContent(String totalCommand) {
        int len = totalCommand.length();
        String result = "";
        result = totalCommand.substring(2, len);
        result = result.substring(0, result.length() - 4);
        return result;
    }
}
